// pages/match/match.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topMenu: ["年化收益", "季收益", "月收益", "日收益", "回撒率", "持仓股数", "价格"],
    topMenu: [
      { id: '0', t: '年化收益' },
      { id: '1', t: '季收益' },
      { id: '2', t: '月收益' },
      { id: '3', t: '日收益' },
      { id: '4', t: '回撒率' },
      { id: '5', t: '持仓股数' },
      { id: '6', t: '价格' },
    ],
    testMenu: [
      { user: '一号策略', nhsy: '12.25%', jsy: '10.25%', ysy: '2.25%', rsy: '10.25%', hcl: '0.25%', ccgs: '100/股', jg: '100' },
      { user: '二号策略', nhsy: '20.01%', jsy: '17.77%', ysy: '6.89%', rsy: '3.02%', hcl: '0.06%', ccgs: '70/股', jg: '300' },
      { user: '三号策略', nhsy: '11.33%', jsy: '6.75%', ysy: '5.22%', rsy: '9.58%', hcl: '1.09%', ccgs: '200/股', jg: '220' },
    ],
    hidFeet: true,
    startX: null,
    delBtnWidth: 220,
    addText: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  sortMenu:function(e){
    var that = this;
    var menu = that.data.testMenu;
    var compare = function (prop) {
      return function (obj1, obj2) {
        var val1 = obj1[prop];
        var val2 = obj2[prop];
        if (isNaN(Number(val1)) && isNaN(Number(val2))) {
          val1 = parseFloat(val1);
          val2 = parseFloat(val2);
        }
        if (val1 < val2) {
          return -1;
        } else if (val1 > val2) {
          return 1;
        } else {
          return 0;
        }
      }
    }
    var nono = menu.sort(compare(e.currentTarget.id));
    that.setData({
      testMenu: nono
    })
  },

  Showfeet: function(e){
    var that = this;
    //获取手指触摸的是哪一项
    var index = e.currentTarget.dataset.index;
    var list = that.data.testMenu;
    that.setData({
      addText: list[index].user
    });
    this.setData({
      hidFeet: false
    })
  },

  cancel:function(){
    this.setData({
      hidFeet: true
    })
  },

  like:function(){
    wx.showToast({
      title: '收藏成功',
      icon: 'success',
    });
    setTimeout(function(){
      wx.hideToast();
    },1500)
  },

  book: function () {
    wx.showToast({
      title: '订阅成功',
      icon: 'success',
    });
    setTimeout(function () {
      wx.hideToast();
    }, 1500)
  },

  touchS: function (e) {
    //判断是否只有一个触摸点
    if (e.touches.length == 1) {
      this.setData({
        //记录触摸起始位置的X坐标
        startX: e.touches[0].clientX
      });
    }
  },
  //触摸时触发，手指在屏幕上每移动一次，触发一次
  touchM: function (e) {
    var that = this
    if (e.touches.length == 1) {
      //记录触摸点位置的X坐标
      var moveX = e.touches[0].clientX;
      //计算手指起始点的X坐标与当前触摸点的X坐标的差值
      var disX = moveX - that.data.startX;
      //delBtnWidth 为右侧按钮区域的宽度
      // var delBtnWidth = that.data.delBtnWidth;
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        
      } else if (disX > 0) {//移动距离大于0，文本层露出值等于手指移动距离
        if (that.data.x >= 0){
          //按钮全部显示后不可继续超出滑动
        }else{
          that.setData({
            x: that.data.x + disX
          });
        }      
      }
      //获取手指触摸的是哪一个item
      var index = e.currentTarget.dataset.index;
      var list = that.data.testMenu;
      //将拼接好的样式设置到当前item中
      list[index].x = that.data.x;
      //更新列表的状态
      this.setData({
        testMenu: list
      });
    }
  },
  touchE: function (e) {
    var that = this
    if (e.changedTouches.length == 1) {
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = endX - that.data.startX;
      // var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      if (disX > 20) {
        this.setData({
          x: 0
        })
      }else{
        this.setData({
          x: -220
        })
      } 
    }
    //获取手指触摸的是哪一项
    var index = e.currentTarget.dataset.index;
    var list = that.data.testMenu;
    list[index].x = that.data.x;
    //更新列表的状态
    that.setData({
      testMenu: list
    });
  },
  redo:function(e){
    var that = this;
    //获取手指触摸的是哪一项
    var index = e.currentTarget.dataset.index;
    var list = that.data.testMenu;
    list[index].x = -220;
    //更新列表的状态
    that.setData({
      testMenu: list
    });
  },
  Delete:function(e){
    var that = this;
    //获取手指触摸的是哪一项
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定移除当前策略？',
      confirmText: '移除',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          var list = that.data.testMenu;
          list[index].x = -220;
          that.setData({
            testMenu: list
          });
        }
      }
    });
  }

})